#include "vdata_modbus_slave.h"

char input_hex_str[500];
uint8_t modub_hex_input[100];
uint16_t input_length;
vdata_modbus_slave_t vdata_modbus_slave;

uint16_t vdata_str_to_arry(char *str, uint8_t *hex_arry)
{
    char *src = str;
    uint8_t *dst = hex_arry;
    unsigned int hex_value = 0, size = 0;
    while(*src) {
        if(' ' == *src) {
            src++;
            continue;
        }
        sscanf(src, "%02X", &hex_value);
        *dst = (uint8_t)hex_value;
        size ++;
        src += 2;
        dst++;
    }
    return size;
}

int main(void)
{
    uint16_t crc_16 = 0;

    while(1){
        printf("\r\nMaster Request:\r\n");
        gets(input_hex_str);

        input_length = vdata_str_to_arry(input_hex_str, modub_hex_input);
        crc_16 = modbus_crc16(modub_hex_input, input_length);
        modub_hex_input[input_length] = (crc_16 >> 8) & 0xFF;
        modub_hex_input[input_length + 1] = crc_16 & 0xFF;
        input_length += 2;
        vdata_modbus_slave_parse(&vdata_modbus_slave, modub_hex_input, input_length);
    }
    return 0;
}
